package com.cfwx.thread;

/**
 * 死锁示例
 * @author Administrator
 *
 */
public class DeadLock implements Runnable{
    static Object A = new Object();//注：必须是静态资源
    static Object B = new Object();
	int flag = 1;
    
	public void run(){
		if (flag == 1){
			synchronized(A){
				System.out.println("得到资源A,若再得到资源B就能完成任务");
				synchronized(B){
					System.out.println("得到资源B，运行结束");
				}
			}
			
		}
		
		if (flag == 0){
			synchronized(B){
				System.out.println("得到资源B，若再得到资源A就能完成任务");
				synchronized(A){
					System.out.println("得到资源A，运行结束");
				}
			}
		}
		
	}
	
	
	public static void main(String args[]){
		DeadLock deadLock1 =  new DeadLock();
		DeadLock deadLock2 =  new DeadLock();
		deadLock1.flag=1;
		deadLock2.flag=0;
		Thread t1 = new Thread(deadLock1);
		Thread t2 = new Thread(deadLock2);
		t1.start();
		t2.start();
	}
	
	

}
